package com.kinon.ycjshop.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.kinon.ycjshop.Constatus;
import com.kinon.ycjshop.R;
import com.kinon.ycjshop.tools.SPManage;
import com.kinon.ycjshop.tools.Url;

public class WebFragment extends Fragment {

    private View mView;
    private WebView web_view;


    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_web, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initVew();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            showWeb();
        }
    }

    private void initVew() {
        web_view = mView.findViewById(R.id.web_view);
        showWeb();
    }

    private void showWeb() {
        SharedPreferences spuser = getActivity().getSharedPreferences("spuser", Context.MODE_PRIVATE);
//        String url = SPManage.getInstance(getActivity()).getString("serverUrl") +
        String url = "http://wjdc.zjyc.cn/zhst/manager/index.php/index_admin/index?username=" +
                spuser.getString("phone", "") +
                "&password=" +
                spuser.getString("pwd", "");
        Log.e("lh", "url== " + url);
        web_view.loadUrl(url);
        web_view.setWebViewClient(new WebViewClient() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                web_view.loadUrl(request.getUrl().toString());
                Log.e("lh", request.toString());
                return true;
            }
        });
//        web_view.loadUrl(Url.ServerUrl+"home/index"); // 进销存管理系统

        WebSettings settings = web_view.getSettings();
        // 缓存(cache)
        settings.setAppCacheEnabled(true);
//        settings.setAppCachePath(context.getCacheDir().getAbsolutePath());
        // 存储(storage)
        settings.setDomStorageEnabled(true);    // 默认值 false
        settings.setDatabaseEnabled(true);      // 默认值 false
        //自适应手机屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        // 是否支持Javascript，默认值false
        settings.setJavaScriptEnabled(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 5.0以上允许加载http和https混合的页面(5.0以下默认允许，5.0+默认禁止)
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);

    }
}
